﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace WPKeyboard
{
    public partial class SettingsPage : PhoneApplicationPage
    {
        AppSettings settings;

        public SettingsPage()
        {
            InitializeComponent();

            // Initialize an AppSettings object
            settings = new AppSettings();

            LoadSetttings();
        }

        private void LoadSetttings()
        {
            // Loading the Play setting
            chkPlaySound.IsChecked = settings.IsPlayClickSoundChecked;

            // Loading the Vibrate setting
            chkVibrate.IsChecked = settings.IsVibrationChecked;

            List<LanguageProperties> KeyboardLanguages = new List<LanguageProperties>(KeyboardInfoUtilities.LanguagesProperties.Values);

            // Loading the Languages settings to the page
            bool isLanguageChecked;
            foreach (LanguageProperties language in KeyboardLanguages)
            {
                // Get the language setting if exist, if else it won't be selected.
                isLanguageChecked = settings.GetOtherKeyboardChecked(language.Caption + AppSettings.constKeyboardChecked);

                lstLanguages.Items.Add(new CheckBox() { Content = language.Caption, IsChecked = isLanguageChecked });
            }
        }

        void cancelButton_Click(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }

        void doneButton_Click(object sender, EventArgs e)
        {
            // Saving the Play setting
            settings.IsPlayClickSoundChecked = chkPlaySound.IsChecked.Value;

            // Saving the Vibrate setting
            settings.IsVibrationChecked = chkVibrate.IsChecked.Value;

            // variable for checking that at list one language item has been checked
            bool AnyItemChecked = false;

            // Save the languages list settings
            foreach (CheckBox languageItem in lstLanguages.Items)
            {
                // Get the language setting from the dynamic listbox
                AnyItemChecked |= (bool)languageItem.IsChecked;

                // Set the value to the app settings
                settings.SetOtherKeyboardChecked(languageItem.Content + AppSettings.constKeyboardChecked, languageItem.IsChecked.Value);
            }

            if (AnyItemChecked)
            {
                // Save the settings
                settings.Save();

                // Navigate back to the keyboard
                NavigationService.GoBack();
            }
            else
            {
                MessageBox.Show("At list one item has to be checked in order to save.");
            }
        }
    }
}